package com.sgb.kjwl.viewmodel.self;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.AndroidViewModel;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.self.CarInfoEntity;
import com.sgb.kjwl.view.ui.activity.self.AddCarInfoNextActivity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddCarInfoNextModel extends AndroidViewModel {
    public ObservableField<CarInfoEntity> carInfoEntity = new ObservableField<>(new CarInfoEntity());
    public AddCarInfoNextModel(@NonNull Application application) {
        super(application);
    }
    //上传图片
    public void uploadMutiFileModel(AddCarInfoNextActivity activity, File file, int type) {
        Map<String, RequestBody> photos = new HashMap<>();
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" +  "image.jpg\"", requestBody);
        DialogHelper.showProgressDialog(activity, null, "文件上传中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getUploadAPI().uploadFile(photos).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.uploadImgSuccess(response.body().getData().toString(), type);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });
    }
    //上传多图片
    public void uploadMutiFileModels(AddCarInfoNextActivity activity, List<File> files) {
        Map<String, RequestBody> photos = new HashMap<>();
        int i = 0;
        for (File file : files) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            photos.put("file" + "\"; filename=\"" + i + "image.jpg\"", requestBody);
            i++;
        }
       /* RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" +  "image.jpg\"", requestBody);*/
        DialogHelper.showProgressDialog(activity, null, "文件上传中...", 0, false, null).setCanceledOnTouchOutside(true);
     /*   APIManager.getInstance().getUploadAPI().uploadMutiFileDrive(photos).enqueue(new Callback<BaseEntity<List<String>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<String>>> call, Response<BaseEntity<List<String>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    //activity.uploadImgSuccess(response.body().getData().toString(), type);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<String>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });*/
    }

    public void authCar(AddCarInfoNextActivity activity){
        DialogHelper.showProgressDialog(activity, null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        Map<String, Object> requestUtil = new HashMap<>();
  /*      if(carInfoEntity.get().getId()!=0) {
            requestUtil.put("id", carInfoEntity.get().getId());
        }
        requestUtil.put("telphone",carInfoEntity.get().getTelphone());
        requestUtil.put("vehicleLicense",carInfoEntity.get().getVehicleLicense());
        requestUtil.put("drivingLicensePage",carInfoEntity.get().getDrivingLicensePage());
        requestUtil.put("carNumber",carInfoEntity.get().getCarNumber());
        requestUtil.put("carTypeNo",carInfoEntity.get().getCarTypeNo());
        requestUtil.put("carLenNo",carInfoEntity.get().getCarLen());
        requestUtil.put("weight",carInfoEntity.get().getWeight());
        requestUtil.put("licensePlateColor",carInfoEntity.get().getLicensePlateColor());
        requestUtil.put("provinceNo",carInfoEntity.get().getProvinceNo());
        requestUtil.put("provinceName",carInfoEntity.get().getProvinceName());
        requestUtil.put("cityNo",carInfoEntity.get().getCityNo());
        requestUtil.put("cityName",carInfoEntity.get().getCityName());
        requestUtil.put("districtNo",carInfoEntity.get().getDistrictNo());
        requestUtil.put("districtName",carInfoEntity.get().getDistrictName());
        requestUtil.put("detailAddress",carInfoEntity.get().getDetailAddress());
        requestUtil.put("longitude",carInfoEntity.get().getLongitude());
        requestUtil.put("latitude",carInfoEntity.get().getLatitude());
        requestUtil.put("carImage",carInfoEntity.get().getCarImage());
        requestUtil.put("transportCertificateMain",carInfoEntity.get().getTransportCertificateMain());*/




 /*       APIManager.getInstance().getDriverAPI().authCar(carInfoEntity.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if(response.body()==null){
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    EventBus.getDefault().post(LogisticsEvent.make().setMessageTag(EventConstant.EVENT_CARINFO_COMMIT));
                    activity.finish();
                    MToast.showToast(activity, "提交成功");

                }else{
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity, t.getMessage());
            }
        });*/

    }
}
